package utils;

import android.app.Application;
import android.graphics.Bitmap;
import android.os.Environment;
import android.widget.ImageView;

import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiskCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.qianfeng.lingshijia.R;

import java.io.File;

/**
 * 下载工具包
 */
public class DownloadUtils {

    private final static String CODE_MODE = "utf-8";//编码格式
    private final static int THREAD_NUMBER = 5;//线程数量
    private final static int REQUEST_NUMBER = 3;// 设请求重试次数
    private final static int REQUEST_TIME = 6*1000; // 设置超时时间

    private static HttpUtils httpUtils;
    private static ImageLoader imageLoader;
    private static Application application;
    private static DisplayImageOptions options;

    public static boolean initHttpUtils(Application app) {
        application = app;
        if (httpUtils == null) {
            httpUtils = new HttpUtils(CODE_MODE);
            httpUtils.configRequestThreadPoolSize(THREAD_NUMBER);
            httpUtils.configRequestRetryCount(REQUEST_NUMBER);
            // 设置响应的编码
            httpUtils.configResponseTextCharset(CODE_MODE);
            httpUtils.configSoTimeout(REQUEST_TIME);
            return true;
        }
        return false;
    }

    public static void getJson(String url, RequestCallBack<String> callBack)
    {
        httpUtils.send(HttpRequest.HttpMethod.GET,url,callBack);
    }

    public static void postJson(String url, RequestCallBack<String> callBack,RequestParams params){
        httpUtils.send(HttpRequest.HttpMethod.POST, url, params,callBack);
    }

    public static HttpUtils getHttpUtils() {
        return httpUtils;
    }


    public static void initImageLoader() {
        if (imageLoader == null) {
            //创建ImageLoader,使用getInstance()
            imageLoader = ImageLoader.getInstance();

            //定义磁盘缓存路径
            String diskPath = null;
            if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
                diskPath = new StringBuffer()
                        .append(Environment.getExternalStorageDirectory())
                        .append(File.separator)
                        .append("qianfeng/imageCache").toString();
                // /mnt/sdcard/qianfeng/imageCache
            } else {
                diskPath = new StringBuffer().append(Environment.getDataDirectory())
                        .append(File.separator)
                        .append(application.getPackageName())
                        .append("qianfeng/imageCache").toString();
                // /data/data/com.example.volley_case_greentree_list/qianfeng/imageCache
            }
            //定义内存缓存大小
            // 设置内存缓存
            int cacheSize = (int) Runtime.getRuntime().maxMemory() / 6;

            // 创建ImageLoader配置
            ImageLoaderConfiguration configuration =
                    new ImageLoaderConfiguration.Builder(application)
                            .threadPoolSize(5) // 设置线程池线程大小
                                    // 磁盘缓存位置
                            .diskCache(new UnlimitedDiskCache(new File(diskPath)))
                            .diskCacheSize(100 * 1024 * 1024) // 磁盘缓存的大小 60M
                            .diskCacheFileCount(400) // 磁盘缓存的文件数量
                                    // 可以设置自定义内存缓存，并且自定义缓存大小
                                    //.memoryCache(new LruMemoryCache(5 * 1024 * 1024))
                            .memoryCacheSize(cacheSize) // 设置内存缓存大小
                            .build();
            imageLoader.init(configuration);
            // 在全局设置图片显示选项
            options = new DisplayImageOptions.Builder()
                    .cacheInMemory(false) // 是否缓存到内存
                    .cacheOnDisk(true) // 是否缓存到磁盘
                    .showImageOnLoading(R.drawable.register_bg_img) // 加载显示的图片
                    .showImageOnFail(R.drawable.register_notice_img) // 加载失败显示的图片
                    .bitmapConfig(Bitmap.Config.ARGB_4444) // 设置图片属性 RGB_565 无效，4.0后无效
                    .imageScaleType(ImageScaleType.IN_SAMPLE_INT)
                    .build();
        }
    }

    /**
     * 加载图片接口，自带三级缓存
     * @param uri 图片地址
     * @param loadedImageView 需要加载图片的ImageView
     */
    public static void LoadPicture(String uri,ImageView loadedImageView){
        imageLoader.displayImage(uri,loadedImageView,options);
    }
}
